﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeLinkApi.Models.Response;
using Newtonsoft.Json;
using WeLinkApi.Models.Request;

namespace WeLinkApi
{
    public class WeLinkApi
    {
        private static readonly string meetingsUrl = ConfigurationManager.AppSettings["WelinkMeetingUrl"];
        private static readonly WeLinkApiAction Action = new WeLinkApiAction(meetingsUrl);

        #region WeLink会议
        /// <summary>
        /// 通过账号密码获取token
        /// </summary>
        /// <param name="request">body参数</param>
        /// <returns></returns>
        public M_accessTokeninfoResponse GetMeetingToken(M_proxyRequest request)
        {
            M_accessTokeninfoResponse response = Action.Execute<M_accessTokeninfoResponse>(request);
            return response;
        }

        /// <summary>
        /// 会议预约
        /// </summary>
        /// <param name="request"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public M_ConferenceInfoResponse BookConferences(M_RestScheduleRequest request, string token)
        {
            M_ConferenceInfoResponse response = Action.ExecuteAction<M_ConferenceInfoResponse>(request, token);
            return response;
        }

        /// <summary>
        /// 获取会议列表
        /// </summary>
        /// <returns></returns>
        public M_RestListConfResponse GetMeetingsList(M_RestLisrConfRequest request, string token)
        {
            M_RestListConfResponse response = Action.ExecuteActionNoReq<M_RestListConfResponse>(request, token);
            return response;
        }

        /// <summary>
        /// 获取会议信息
        /// </summary>
        /// <returns></returns>
        public M_RestQueryConfDetailResponse GetMeetingsInfo(M_ConfInfoRequest request, string token)
        {
            M_RestQueryConfDetailResponse response = Action.ExecuteActionNoReq<M_RestQueryConfDetailResponse>(request, token);
            return response;
        }


        /// <summary>
        /// 取消预约会议
        /// 会议还未开始
        /// </summary>
        /// <returns></returns>
        public M_RestResponse DeleteBookMeetings(M_DeleteBookMeetingRequest request, string token)
        {
            M_RestResponse response = Action.ExecuteDeleteActionNoReq<M_RestResponse>(request, token);
            return response;
        }

        /// <summary>
        /// 修改预约会议
        /// 会议还未开始
        /// </summary>
        /// <returns></returns>
        public M_UpdateBookMeetingResponse UpdateBookMeetings(M_UpdateBookMeetingRequest request, string token)
        {
            M_UpdateBookMeetingResponse response = Action.ExecutePutActionNoReq<M_UpdateBookMeetingResponse>(request, token);
            return response;
        }
        #endregion

        #region WeLink通讯录
        /// <summary>
        /// 获取AccessToken
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public  W_AccessTokenResponse GetAccessTokenByID(W_AccessTokenRequest request)
        {
            W_AccessTokenResponse accessToken = Action.ExecuteUnit<W_AccessTokenResponse>(request);
            return accessToken;
        }
        /// <summary>
        /// 获取用户详情
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public W_UserInfoResponse GetUserInfoByToken(W_UserInfoRequest request,string token )
        {
            W_UserInfoResponse userInfo = Action.ExecuteUnitND<W_UserInfoResponse>(request,token); 
            return userInfo;
        }
        /// <summary>
        /// 查询用户父级部门
        /// </summary>
        /// <param name="request">根据用户信息（userId、corpUserId、mobileNumber）获取用户所在部门的所有上级父部门信息。</param>
        /// <returns></returns>
        public W_UserAllParentResponse GetUserAllParentDept(W_UserAllParentRequest request, string token)
        {
            W_UserAllParentResponse resp = Action.ExecuteUnitGet<W_UserAllParentResponse>(request, token);
            return resp;
        }

        /// <summary>
        /// 查询部门的人员信息列表(详细信息)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public W_UserInfoOfDeptResponse GetUserInfoDetailOfDept(W_UserInfoOfDeptRequest request, string token)
        {
            W_UserInfoOfDeptResponse response = Action.ExecuteUnitGet<W_UserInfoOfDeptResponse>(request,token);
            return response;
        }

        /// <summary>
        /// 获取部门信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public W_DeptmentInfoResponse GetDeptmentInfo(W_DeptmentInfoRequest request,string token)
        {
            W_DeptmentInfoResponse response = Action.ExecuteUnitND<W_DeptmentInfoResponse>(request,token);
            return response;
        }

        /// <summary>
        /// 获取父部门
        /// </summary>信息
        /// <param name="request"></param>
        /// <returns></returns>
        public W_ParentDeptResponse GetParentDeptInfo(W_ParentDeptRequest request,string token)
        {
            W_ParentDeptResponse resp = Action.ExecuteUnitGet<W_ParentDeptResponse>(request,token);
            return resp;
        }
        /// <summary>
        /// 获取子部门信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public W_ChildDeptResponse GetChildDeptInfo(W_ChildDeptRequest request,string token)
        {
            W_ChildDeptResponse resp = Action.ExecuteUnitND<W_ChildDeptResponse>(request,token);
            return resp;
        }
        #endregion

        #region 异步批量同步用户部门
        /// <summary>
        /// 异步批量同步用户
        /// 接受用户信息，异步同步到WeLink通讯录，支持批量更新用户信息，每次请求最多支持10个用户。
        /// </summary>
        public W_UploadUsersByAsynResponse UploadUsersByAsyn(W_UploadUsersByAsynRequest request,string token)
        {
            W_UploadUsersByAsynResponse resp = Action.ExecuteActionAsyn<W_UploadUsersByAsynResponse>(request,token);
            return resp;
        }



        /// <summary>
        /// 异步批量同步部门
        /// 接受部门信息，同步到WeLink通讯录，支持批量更新部门信息，每次请求最多支持10个部门。
        /// </summary>
        public W_UploadDeptsByAsynResponse UploadDeptsByAsyn(W_UploadDeptsByAsynRequest request,string token)
        {
            W_UploadDeptsByAsynResponse resp = Action.ExecuteActionAsyn<W_UploadDeptsByAsynResponse>(request,token);
            return resp;
        }


        #endregion

        public T DeserializeObject<T>(string jsonResult)
        {
            return JsonConvert.DeserializeObject<T>(jsonResult);
        }

        public string SerializeObject<T>(T obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

    }
}
